/*
 * 文件名： FavorFragment.java
 * 描    述：我的收藏公共Fragment
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity.fragment;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

import com.miguo.activity.base.BaseFragment;
import com.miguo.activity.callback.IGoodsScroll;
import com.miguo.activity.callback.listener.GoodsScrollListener;
import com.miguo.adapter.FavorAdapter;
import com.miguo.app.AppContext;
import com.miguo.bean.GoodsBean;
import com.miguo.db.GoodsDB;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.GridViewWithHeaderAndFooter;
import com.miguo.framework.view.pulltorefresh.PullToRefreshBase;
import com.miguo.framework.view.pulltorefresh.PullToRefreshBase.OnRefreshListenerTwo;
import com.miguo.framework.view.pulltorefresh.PullToRefreshGridView;
import com.miguo.ui.R;
import com.miguo.utils.CommonUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class FavorFragment extends BaseFragment implements OnClickListener,
        OnRefreshListenerTwo<GridViewWithHeaderAndFooter>, IGoodsScroll {

    private View view;
    private GridViewWithHeaderAndFooter gridView;
    private ImageView mToTopBtn;
    private ImageView mNolistImg;
    private int type;
    private int page = 1;
    private int pageSize = 20;
    private boolean endlist;
    private boolean isRefresh = true;
    private List<GoodsBean> arrayList;
    private FavorAdapter listViewAdapter;
    private PullToRefreshGridView pullToRefresh;

    private Animation mTopUpAnima;
    private Animation mTopDownAnima;

    private boolean pauseOnScroll = false;

    private boolean pauseOnFling = true;

    private ImageLoader imageLoader = ImageLoader.getInstance();

    public static FavorFragment newInstance(int type) {
        FavorFragment fragment = new FavorFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initExtra();
        initView();
    }

    private void initExtra() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getInt("type");
        }
    }

    private void initView() {

        pullToRefresh = (PullToRefreshGridView) view.findViewById(R.id.pull_refresh_grid);
        gridView = pullToRefresh.getRefreshableView();
        mToTopBtn = (ImageView) view.findViewById(R.id.toTopBtn);
        mNolistImg = (ImageView) view.findViewById(R.id.item_nolist_img);
        mToTopBtn.setOnClickListener(this);

        mTopUpAnima = new TranslateAnimation(0.0F, 0.0F, CommonUtils.dip2px(
                AppContext.getContext(), 48.0F), 0.0F);
        mTopUpAnima.setDuration(500L);
        mTopDownAnima = new TranslateAnimation(0.0F, 0.0F, 0.0F, CommonUtils.dip2px(
                AppContext.getContext(), 48.0F));
        mTopDownAnima.setDuration(500L);

        arrayList = new ArrayList<GoodsBean>();
        // 使用自定义的Adapter
        listViewAdapter = new FavorAdapter(mActivity);
        gridView.setAdapter(listViewAdapter);
        gridView.setOnScrollListener(new GoodsScrollListener(this));
        // 列数
        gridView.setNumColumns(2);

        // 滑动到顶部和底部时出现的阴影消除 2.3以前用 android:fadingEdge="none"
        if (Build.VERSION.SDK_INT >= 9) {
            gridView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        }

        // 去除上拉下拉的箭头
        pullToRefresh.setShowIndicator(false);
        // 设置监听器
        pullToRefresh.setOnRefreshListener(this);
        getData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toTopBtn:
                gridView.setSelection(0);
                mToTopBtn.setVisibility(View.GONE);
                break;
        }
    }

    private void LoadData() {
        if (pageSize >= page + 1) {
            page++;
            isRefresh = false;
            getData();
        } else {
            Toast.show(mContext, "已经到底了!");
            pullToRefresh.onRefreshComplete();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<GridViewWithHeaderAndFooter> refreshView) {
        page = 1;
        isRefresh = true;
        getData();

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<GridViewWithHeaderAndFooter> refreshView) {
        LoadData();
    }

    protected void getData() {
        if (endlist) {
            pullToRefresh.setVisibility(View.VISIBLE);
        }

        pullToRefresh.onRefreshComplete();

        arrayList = GoodsDB.getInstance(mContext, true).query(page, pageSize,
                type);
        if (arrayList != null && arrayList.size() > 0) {
            listViewAdapter.addAll(arrayList, isRefresh);
        } else if (page == 1) {
            setNoListText("");
        } else {
            page--;
            Toast.show(mContext, CommonUtils.getString(mContext,
                    R.string.pull_to_refresh_nodata));
        }
        arrayList.clear();
        endlist = false;
    }

    private void setNoListText(String tips) {
        pullToRefresh.setVisibility(View.GONE);
        mNolistImg.setVisibility(View.VISIBLE);
        if (type == 1) {
            mNolistImg.setBackgroundResource(R.drawable.ic_favor_default_noti);
            return;
        }
        mNolistImg.setBackgroundResource(R.drawable.ic_favor_default_all);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_favor, container, false);
        return view;
    }

    @Override
    public void onScrollIdle() {
        imageLoader.resume();
    }

    @Override
    public void onScrollFling() {
        if (pauseOnFling) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTouchScroll() {
        if (pauseOnScroll) {
            imageLoader.pause();
        }
    }

    @Override
    public void onScrollTopPositiont() {
        // 判断滚动到顶部
        if (mToTopBtn.getVisibility() == View.VISIBLE) {
            mToTopBtn.setVisibility(View.GONE);
            mToTopBtn.startAnimation(mTopDownAnima);
        }
    }

    @Override
    public void onScrollFirstLastPosition() {
        // 第一次滚动至底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
    }

    @Override
    public void onScrollLastPosition() {
        // 判断滚动到底部
        if (mToTopBtn.getVisibility() == View.GONE) {
            mToTopBtn.setVisibility(View.VISIBLE);
            mToTopBtn.startAnimation(mTopUpAnima);
        }
        LoadData();
    }
}
